﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Xin478.CSWork.Client
{
    public class MyItem : Control, IDisposable
    {
        public Color BorderColor { get; set; }
        public Color BackHoverColor { get; set; }

        public MyItem()
        {
            this.BorderColor = Color.FromArgb(188, 206, 232);
            this.BackHoverColor = Color.FromArgb(228, 236, 242);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            drawWord(e.Graphics, e.ClipRectangle);
            base.OnPaint(e);
        }

        void drawWord(Graphics graphics, Rectangle rect)
        {
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Center;
            sf.Trimming = StringTrimming.EllipsisCharacter;
            Rectangle newRect = new Rectangle(rect.Left + 2, rect.Top + 2, rect.Width - 4, rect.Height - 4);
            using (Brush b = new SolidBrush(this.ForeColor))
            {
                graphics.DrawString(this.Text, this.Font, b, newRect, sf);
            }
        }

        protected override void OnMouseHover(EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();
            Rectangle rect = this.ClientRectangle;
            using (Brush tmpb = new SolidBrush(this.BackHoverColor))
            {
                graphics.FillRectangle(tmpb, rect);
            }
            using (Pen p = new Pen(this.BorderColor, 1))
            {
                graphics.DrawRectangle(p, 0, 0, rect.Width - 1, rect.Height - 1);
            }
            drawWord(graphics, rect);
            base.OnMouseHover(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();
            Rectangle rect = this.ClientRectangle;
            using (Brush tmpb = new SolidBrush(this.BackColor))
            {
                graphics.FillRectangle(tmpb, rect);
            }
            drawWord(graphics, rect);
            base.OnMouseLeave(e);
        }

        public event EventHandler Closed;

        public void Close()
        {
            this.Visible = false;
            if (this.Closed != null) this.Closed(this, new EventArgs());
            this.Dispose();
        }

    }
}
